package com.bysc.userservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bysc.baseservice.handler.BaiYunException;
import com.bysc.commonutils.ResultCode;
import com.bysc.commonutils.Results;
import com.bysc.commonutils.constant.MessageConstant;
import com.bysc.commonutils.enums.NoticeEnum;
import com.bysc.commonutils.utils.JwtUtils;
import com.bysc.userservice.entity.NoticeContent;
import com.bysc.userservice.service.NoticeContentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

import static com.bysc.commonutils.enums.NoticeEnum.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author kkk
 * @since 2024-05-28
 */
@RestController
@Api(tags = "通知管理")
@RequestMapping("/noticeservice/notice-content")
public class NoticeContentController {
    @Autowired
    private NoticeContentService noticeContentService;

    @ApiOperation(value = "根据token批量更新通知已读状态")
    @PutMapping("updateReadStatusByToken")
    public Results updateReadStatus(HttpServletRequest request) {
        try {
            String userId = JwtUtils.getMemberIdByJwtToken(request);
            QueryWrapper<NoticeContent> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("noticed_user_id", userId);
            queryWrapper.eq("is_read", 0);
            NoticeContent noticeContent = new NoticeContent();
            noticeContent.setIsRead(1);
            noticeContentService.update(noticeContent, queryWrapper);
            return Results.ok().message(MessageConstant.EDIT_NOTICE_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.EDIT_NOTICE_FAIL);
        }
    }

    //分页带条件查询通知
    @ApiOperation(value = "（带条件）获取用户通知列表")
    @PostMapping("getNoticesList/{page}/{limit}/{type}")
    public Results getUserNotices(@PathVariable Integer page, @PathVariable Integer limit, @PathVariable Integer type, HttpServletRequest request) {
        try {
            String userId = JwtUtils.getMemberIdByJwtToken(request);
            QueryWrapper<NoticeContent> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("noticed_user_id", userId);
            switch (NoticeEnum.getByValue(type)) {
                case SYSTEM:
                    queryWrapper.eq("type", SYSTEM.getValue());
                    break;
                case CLUB:
                    queryWrapper.eq("type", CLUB.getValue());
                    break;
                case LIKE:
                    queryWrapper.eq("type", LIKE.getValue());
                    break;
                case COLLECT:
                    queryWrapper.eq("type", COLLECT.getValue());
                    break;
                case COMMENT:
                    queryWrapper.eq("type", COMMENT.getValue());
                    break;
            }
            queryWrapper.orderByDesc("gmt_create");
            IPage<NoticeContent> noticePage = noticeContentService.page(new Page<>(page, limit), queryWrapper);
            return Results.ok().data("notices", noticePage).message(MessageConstant.QUERY_NOTICE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.QUERY_NOTICE_FAIL);
        }
    }

    //获取通知数量（总数量，包括：系统、协会,点赞、收藏、评论的通知数量）
    // 获取用户通知总数
    @ApiOperation(value = "获取用户的通知总数")
    @GetMapping("getCountByToken")
    public Results getNotificationCounts(HttpServletRequest request) {
        try {
            String userId = JwtUtils.getMemberIdByJwtToken(request);
            List<NoticeEnum> types = Arrays.asList(SYSTEM, CLUB, LIKE, COLLECT, COMMENT);
            int total = 0, system = 0, club = 0, like = 0, collect = 0, comment = 0;
            for (NoticeEnum type : types) {
                QueryWrapper<NoticeContent> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("noticed_user_id", userId);
                queryWrapper.eq("type", type.getValue());
                switch (type) {
                    case SYSTEM:
                        system = noticeContentService.count(queryWrapper);
                        break;
                    case CLUB:
                        club = noticeContentService.count(queryWrapper);
                        break;
                    case LIKE:
                        like = noticeContentService.count(queryWrapper);
                        break;
                    case COLLECT:
                        collect = noticeContentService.count(queryWrapper);
                        break;
                    case COMMENT:
                        comment = noticeContentService.count(queryWrapper);
                        break;
                }
            }
            total = system + club + like + collect + comment;
            return Results.ok().data("total", total).data("system", system).data("club", club).data("like", like).data("collect", collect).data("comment", comment).message(MessageConstant.QUERY_NOTICE_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.QUERY_NOTICE_FAIL);
        }

    }

}

